<?php

class Model_Card
{

    /**
     * Create new card and assign to account
     * @param unknown_type $params
     * @throws Zend_Db_Exception
     */
    public static function create ($params)
    {
        try
        {            
            $db = Core_Global::getDatabase();                                                
            
            $stmt = $db->prepare('INSERT INTO Card VALUES(:p_CardID, :p_CreateDate, :p_ExpirationDate, :p_Type, :p_Status)');
            $stmt->bindValue(':p_CardID', null);
            $stmt->bindValue(':p_CreateDate', $params['CreateDate']);
            $stmt->bindValue(':p_ExpirationDate', $params['ExpirationDate']);
            $stmt->bindValue(':p_Type', $params['Type']);
            $stmt->bindValue(':p_Status', $params['Status']);
            
            $stmt->execute();

            $cardId = $db->lastInsertId();
            
            $stmt = $db->prepare('UPDATE Account SET CardID = :p_CardID WHERE AccountID = :p_AccountID');
            $stmt->bindValue(':p_CardID', $cardId);
            $stmt->bindValue(':p_AccountID', $params['AccountID']);        
            
            $stmt->execute();
            
            return $cardId;
        }
        catch (Zend_Db_Exception $e)
        {
            throw new Zend_Db_Exception($e);
        }
    }

    public static function edit ($params)
    {
        try
        {
            $db = Core_Global::getDatabase();
            
            $stmt = $db->prepare('UPDATE Card SET 
                                CreateDate = :p_CreateDate, 
                                ExpirationDate = :p_ExpirationDate, 
                                Type = :p_Type, Status = :p_Status                                
                                WHERE CardID = :p_CardID');
            $stmt->bindValue(':p_CardID', $params['CardID']);
            $stmt->bindValue(':p_CreateDate', $params['CreateDate']);
            $stmt->bindValue(':p_ExpirationDate', $params['ExpirationDate']);
            $stmt->bindValue(':p_Type', $params['Type']);
            $stmt->bindValue(':p_Status', $params['Status']);
            
            $stmt->execute();
            
            return $stmt->rowCount();
        }
        catch (Zend_Db_Exception $e)
        {
            throw new Zend_Db_Exception($e);
        }
    }
}
